﻿using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;

namespace WqLunTan.Models.DataBase
{
    /// <summary>
    /// 论坛用户表
    /// </summary>
    public class WqUser : IdentityUser<Guid>
    {
        /// <summary>
        /// QQ用户的OpenId
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string OpenIdQQ { get; set; } = string.Empty;

        /// <summary>
        /// 支付宝用户的OpenId
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string OpenIdAli { get; set; } = string.Empty;

        /// <summary>
        /// 百度用户的OpenId
        /// </summary>
        public uint OpenIdBaidu { get; set; }

        /// <summary>
        /// 微信用户的OpenId
        /// </summary>
        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public string OpenIdWechat { get; set; } = string.Empty;

        /// <summary>
        /// 昵称
        /// </summary>
        [MaxLength(10)]
        [Required(AllowEmptyStrings = true)]
        public string NickName { get; set; } = string.Empty;

        /// <summary>
        /// 是否 超级管理员
        /// </summary>
        public bool IsAdmin { get; set; }

        /// <summary>
        /// 是否可以登录
        /// </summary>
        public bool Enable { get; set; }

        public DateTimeOffset CreateTime { get; set; }

        public DateTimeOffset LastUpdate { get; set; }

        /// <summary>
        /// 登录时间, 用于刷新缓存token, 防止改密之后token一直有效, 检查权限, 以及是否禁止登录 
        /// </summary>
        public DateTimeOffset LoginTime { get; set; }

        /// <summary>
        /// 登录类型
        /// </summary>
        public SupportOpenIdTypes LoginType { get; set; }

        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public new string Email { get; set; } = string.Empty;

        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public new string NormalizedEmail { get; set; } = Guid.NewGuid().ToString("N");

        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public new string NormalizedUserName { get; set; } = Guid.NewGuid().ToString("N");

        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public new string PasswordHash { get; set; } = string.Empty;

        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public new string PhoneNumber { get; set; } = string.Empty;

        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public new string SecurityStamp { get; set; } = string.Empty;

        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public new string UserName { get; set; } = string.Empty;

        [MaxLength(255)]
        [Required(AllowEmptyStrings = true)]
        public new string ConcurrencyStamp { get; set; } = string.Empty;

        /// <summary>
        /// 用户权限
        /// </summary>
        public WqUserAuth UserAuth { get; set; } = WqUserAuth.User;

    }

    /// <summary>
    /// 支持的登录类型, 可以叠加
    /// </summary>
    [Flags]
    public enum SupportOpenIdTypes
    {
        QQ = 1,
        Baidu = 2,
        AliBaba = 4
    }

    /// <summary>
    /// 权限列表
    /// </summary>
    [Flags]
    public enum WqUserAuth
    {
        /// <summary>
        /// 普通用户
        /// </summary>
        User = 0x1,

        /// <summary>
        /// 后台用户
        /// </summary>
        Manage = 0x2,

        /// <summary>
        /// 被授予的管理员权限
        /// </summary>
        Admin = 0x4
    }
}
